﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using Microsoft.Practices.Unity;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel.Tools
{
    public enum ReportState
    {
        NotSelected,
        Selecting,
        Selected
    }

    /// <summary>
    /// View Model for a simple menu
    /// </summary>
    public class ViewModelReportControl : ViewModelBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
     
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

         /// <summary>
        /// Gets or sets the ScrumViewModelData
        /// </summary>
        /// <value>ScrumViewModelData</value>
        [Dependency]
        public ScrumViewModelData ScrumData { get; set; }

        /// <summary>
        /// Gets or sets the Container
        /// </summary>
        /// <value>Container</value>
        [Dependency]
        public IUnityContainer Container { get; set; }

        public ReportConfig SelectedReport
        {
            get { return ElementManager.GetReportControl(Name); }
            set
            {
                ElementManager.SetReportControl(Name, value);
                SendPropertyChanged("SelectedReport");
                SendPropertyChanged("HasReport");
                SendPropertyChanged("SelectedReportPair");
            }
        }


        public Pair<Uri, string> SelectedReportPair
        {
            get
            {
                return new Pair<Uri, string>(SelectedReportUri, SelectedReportPost);
            }
        }

        public Uri SelectedReportUri
        {
            get
            {
                var report = ScrumData.CurrentProject.Reports.FirstOrDefault(x => x.Name == SelectedReport.Name);
                if (report != null)
                {
                    return report.FullPath;
                }
                return null;
            }
        }

        public string SelectedReportPost
        {
            get
            {
                var report = ScrumData.CurrentProject.Reports.FirstOrDefault(x => x.Name == SelectedReport.Name);
                if (report != null)
                {
                    /*
                    var paramStart = SelectedReport.Parameter.FirstOrDefault(x => x.Left == "StartDateParam");
                    if( paramStart!=null)
                    {
                       paramStart.Right = ScrumData.CurrentIteration.StartTime.ToString(CultureInfo.InvariantCulture);
                    }
                    var paramEnd = SelectedReport.Parameter.FirstOrDefault(x => x.Left == "EndDateParam");
                    if (paramEnd != null)
                    {
                        paramEnd.Right = ScrumData.CurrentIteration.StartTime.ToString(CultureInfo.InvariantCulture);
                    }
                    */
                    return (report.ToPostCommandArguments((from value in SelectedReport.Parameter select new Pair<string, string>(value.Left, value.Right)).ToList()));
                }
                return null;
            }
        }

        
  
        public ReportState HasReport
        {
            get
            {
                return SelectedReport != null ? ReportState.Selected : ReportState.NotSelected;
            }
        }
        /// <summary>
        /// Name of the control
        /// </summary>
        public string Name { get; set; }
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public ViewModelReportControl()
        {
           
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

 
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        
      

        #endregion

    }
}